Replace string in OLE object

Hi everyone,

 

I'm quite a newbie in DXL but I want to do one that replaces a string inside an OLE object.

 

I manage to execute the Find command with this:

oleGet(objApp, "Selection", objSelection)
oleGet(objSelection, "Find", objFind)
oleMethod(objFind, "ClearFormatting")
olePut(objFind, "Text", sSearchText)
olePut(objFind, "MatchWholeWord", true)
olePut(objFind, "Format", false)
oleMethod(objFind, "Execute")
oleGet(objFind, "Found", bFoundString)

 

But, when I try to perform a Replace with this code:

oleGet(objApp, "Selection", objSelection)
oleGet(objSelection, "Find", objFind)
oleMethod(objFind, "ClearFormatting")
oleGet(objFind, "Replacement", objReplacement)
oleMethod(objReplacement, "ClearFormatting")
olePut(objFind, "Replacement.Text", sReplaceText)
olePut(objFind, "Wrap", "wdFindContinue")
olePut(objFind, "Text", sSearchText)
olePut(objFind, "MatchWholeWord", true)
olePut(objFind, "Format", false)
clear objArgs
put(objArgs, "Replace", "wdReplaceAll")
oleMethod(objFind, "Execute", objArgs)

I get no result in the object text field...

Where is my mistake ?

Thanks by advance for your help,

Julien

 


JulienBaclet - Wed Oct 28 14:53:56 EDT 2015

Re: Replace string in OLE object
DOORSHAM - Thu Oct 29 06:45:53 EDT 2015

Line:    olePut(objFind, "Replacement.Text", sReplaceText)   is the problem.

You did define objReplacement but you never used it in the above line

 

Re: Replace string in OLE object
JulienBaclet - Thu Oct 29 06:50:33 EDT 2015

DOORSHAM - Thu Oct 29 06:45:53 EDT 2015

Line:    olePut(objFind, "Replacement.Text", sReplaceText)   is the problem.

You did define objReplacement but you never used it in the above line

 

Thanks for your reply.

 

I already tried  olePut(objReplacement, "Text", sReplaceText)   without success...

 

Do you see another way to achieve this ?

Re: Replace string in OLE object
DOORSHAM - Thu Oct 29 07:28:14 EDT 2015

Post enough of you code as a file attachment so that others can run the snippet to test.

Re: Replace string in OLE object
JulienBaclet - Thu Oct 29 09:18:08 EDT 2015

DOORSHAM - Thu Oct 29 07:28:14 EDT 2015

Post enough of you code as a file attachment so that others can run the snippet to test.

Here is the code, mostly inspired from an example found on Internet for OLE access.

 

I checked the UserInput with the Find function only, with its Found attribute, and it seems OK.

 

Thanks again for helping me.


Attachments

example_replace_all_in_OLE.dxl

Re: Replace string in OLE object
DOORSHAM - Thu Oct 29 10:17:26 EDT 2015

JulienBaclet - Thu Oct 29 09:18:08 EDT 2015

Here is the code, mostly inspired from an example found on Internet for OLE access.

 

I checked the UserInput with the Find function only, with its Found attribute, and it seems OK.

 

Thanks again for helping me.

Instead of line:   put(objArgs, "Replace", "wdReplaceAll")

try line :     put(objArgs,  "wdReplaceAll") 

This fixes the error:   oleMethodOLE problem: Type mismatch, OLE error code: -2146824070

 

 

Re: Replace string in OLE object
JulienBaclet - Thu Oct 29 10:26:39 EDT 2015

DOORSHAM - Thu Oct 29 10:17:26 EDT 2015

Instead of line:   put(objArgs, "Replace", "wdReplaceAll")

try line :     put(objArgs,  "wdReplaceAll") 

This fixes the error:   oleMethodOLE problem: Type mismatch, OLE error code: -2146824070

 

 

Thanks !

I replace it but it doesn't work on my module...

How did you manage to get the OLE error code ?

Re: Replace string in OLE object
DOORSHAM - Thu Oct 29 12:33:33 EDT 2015

JulienBaclet - Thu Oct 29 10:26:39 EDT 2015

Thanks !

I replace it but it doesn't work on my module...

How did you manage to get the OLE error code ?

Each one of the ole statements usually returns a string -- capture the string then print.

During development most people will need to see the returns to help debug

Re: Replace string in OLE object
JulienBaclet - Fri Oct 30 04:51:30 EDT 2015

DOORSHAM - Thu Oct 29 12:33:33 EDT 2015

Each one of the ole statements usually returns a string -- capture the string then print.

During development most people will need to see the returns to help debug

I didn't find how to do it with Replace function.

Here is my workaround with the Find function...

 

bFoundString = false
oleGet(objApp, "Selection", objSelection)
oleGet(objSelection, "Find", objFind)
oleMethod(objFind, "ClearFormatting")
olePut(objFind, "Text", sSearchText)
olePut(objFind, "MatchWholeWord", true)
olePut(objFind, "Format", false)
oleMethod(objFind, "Execute")
oleGet(objFind, "Found", bFoundString)
if (bFoundString)
{
    oleGet(objApp, "Selection", objSelection)
        olePut(objSelection,"Text",sReplaceText)
}

 

Thanks for helping me

Re: Replace string in OLE object
almserv - Tue Feb 21 02:30:41 EST 2017

JulienBaclet - Fri Oct 30 04:51:30 EDT 2015

I didn't find how to do it with Replace function.

Here is my workaround with the Find function...

 

bFoundString = false
oleGet(objApp, "Selection", objSelection)
oleGet(objSelection, "Find", objFind)
oleMethod(objFind, "ClearFormatting")
olePut(objFind, "Text", sSearchText)
olePut(objFind, "MatchWholeWord", true)
olePut(objFind, "Format", false)
oleMethod(objFind, "Execute")
oleGet(objFind, "Found", bFoundString)
if (bFoundString)
{
    oleGet(objApp, "Selection", objSelection)
        olePut(objSelection,"Text",sReplaceText)
}

 

Thanks for helping me

I was faced with the same issue this week and used your solution as a starting point.

I figured out that the issue with the replace function is that the "wdReplaceAll" token isn't being resolved if sent via the ole automation engine so I sent the integer equivalent which is "2".

You were getting the type mismatch because the ole engine was sending the string "wdReplaceAll" for the "Replace" argument in the execute method and it was expecting an integer.

Since there are multiple optional arguments for the execute method, you have to specify which argument you are sending so you were correct in trying to use put(objArgs, "Replace", "wdReplaceAll") but it needed

to be put(objArgs, "Replace", "2"). If you just use put(objArgs, "wdReplaceAll") then the execute method will take "wdReplaceAll" to be the first argument value -- which is the string to find. So it resolved the ole error but for the wrong reason.

 

oleGet(objApp, "Selection", objSelection)

                  oleGet(objSelection, "Find", objFind)

                  oleMethod(objFind, "ClearFormatting")

                  oleGet(objFind, "Replacement", objReplacement)

                  oleMethod(objReplacement, "ClearFormatting")

                 

                  olePut(objFind, "Text", strSearch)

                  olePut(objReplacement, "Text", strReplace)

                  olePut(objFind, "Forward", true)

                  olePut(objFind, "MatchWholeWord", true)

                  olePut(objFind, "Format", false)

                 

          clear objArgs

                          put(objArgs, "Replace", "2")

                          sErr = oleMethod(objFind, "Execute", objArgs)

                          print sErr "\n"